<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="登录地址" prop="ipaddr">
        <el-input
          v-model="queryParams.ipaddr"
          placeholder="请输入登录地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>

    </el-form>
    <configurable-table
      :columns="tableColumns"
      :data="list"
      :loading="loading"
      :show-pagination="true"
      :current-page.sync="queryParams.pageNum"
      :page-size.sync="queryParams.pageSize"
      :pagination-total="total"
      @page-change="getList">
      <!-- 登录时间列 -->
      <template slot="loginTime" slot-scope="{ row }">
        <span>{{ parseTime(row.loginTime) }}</span>
      </template>

      <!-- 操作列 -->
      <template slot="actions" slot-scope="{ row }">
        <el-button
          size="mini"
          type="text"
          icon="el-icon-delete"
          @click="handleForceLogout(row)"
          v-hasPermi="['monitor:online:forceLogout']"
        >强退</el-button>
      </template>
    </configurable-table>
  </div>
</template>

<script>
import { list, forceLogout } from "@/api/monitor/online";

export default {
  name: "Online",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ipaddr: undefined,
        userName: undefined
      },
      // 表格列配置
      tableColumns: [
        { prop: 'tokenId', label: '会话编号', align: 'center', showOverflowTooltip: true },
        { prop: 'userName', label: '登录名称', align: 'center', showOverflowTooltip: true },
        { prop: 'deptName', label: '部门名称', align: 'center' },
        { prop: 'ipaddr', label: '主机', align: 'center', showOverflowTooltip: true },
        { prop: 'loginLocation', label: '登录地点', align: 'center', showOverflowTooltip: true },
        { prop: 'browser', label: '浏览器', align: 'center' },
        { prop: 'os', label: '操作系统', align: 'center' },
        { prop: 'loginTime', label: '登录时间', align: 'center', minWidth: 180, slot: 'loginTime' },
        { label: '操作', align: 'center', slot: 'actions', fixed: 'right', minWidth: 100 }
      ]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询登录日志列表 */
    getList() {
      this.loading = true;
      list(this.queryParams).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 强退按钮操作 */
    handleForceLogout(row) {
      this.$modal.confirm('是否确认强退名称为"' + row.userName + '"的用户？').then(function() {
        return forceLogout(row.tokenId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("强退成功");
      }).catch(() => {});
    }
  }
};
</script>

